<template>
  <div class="RetrievePassword">
    <NavBar title="">
      <template #left>
        <div class="close" @click="closeRetrievePasswordModal">
          <Icon name="zuojiantou" color="#ccc" size="18" />
        </div>
      </template>
    </NavBar>
    <form class="login-form">
      <h2>找回密码</h2>
      <!-- 输入 xxxx@qq.com 收到的6位验证吗 -->
      <p class="tips">请输入<span>{{ props.email }}</span>收到的6位验证吗</p>
      <div class="login-form-item">
        <input type="text" autocomplete="email" v-model="code" placeholder="请输入邮箱验证码">
        <!-- 获取验证码 -->
        <p class="get-code">获取验证码</p>
      </div>
      <div class="login-form-item">
        <input type="password" autocomplete="new-password" v-model="newPassword" placeholder="请输入新密码">
      </div>
    </form>
    <div class="correct" @click="resetPassword">
      <Icon name="dui" color="#ac5cc5" size="18" />
    </div>
  </div>
</template>

<script setup>
import NavBar from '@/components/Common/NavBar/index.vue'
import Icon from '@/components/Common/Icon/index.vue'
import { onMounted, ref } from 'vue'
const model = defineModel({ default: false })
const closeRetrievePasswordModal = () => model.value = false

// 接收父组件传递的邮箱
const props = defineProps({
  email: {
    type: String,
    default: ''
  }
})
console.log(props.email)
import { userGetCodeAPI, userResetPasswordAPI } from '@/api/Auth'

// 获取验证码
const getCode = async () => {
  const res = await userGetCodeAPI(props.email)
  if (res.code === 200) {
    console.log('验证码发送成功')
  } else {
    console.log('验证码发送失败')
  }
}

// 监听获取验证码按钮点击事件
onMounted(() => {
  getCode()
})
const code = ref('')
const newPassword = ref('')
// 重置密码
const resetPassword = async () => {
  const res = await userResetPasswordAPI({
    email: props.email,
    code: code.value,
    password: newPassword.value
  })
  if (res.code === 200) {
    console.log('密码重置成功')
    // 重置密码成功后，关闭弹窗
    closeRetrievePasswordModal()
  } else {
    console.log('密码重置失败')
  }
}


</script>

<style lang="scss" scoped>
.RetrievePassword {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#6b4bab), to(#bf09db));
  z-index: 1000;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .tips {
    font-size: 12px;
    color: #999;
    margin-bottom: 10px;

    span {
      font-size: 14px;
      color: #f9cd17;
    }
  }

  .login-form {
    min-width: 330px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 20px;
    border-radius: 10px;

    h2 {
      font-size: 20px;
      font-weight: bold;
      line-height: 1;
      margin-bottom: 20px;
      text-align: center;
      color: #ccc;
    }

    .login-form-item {
      display: flex;
      gap: 5px;
      position: relative;


      .get-code {
        font-size: 12px;
        color: #fff;
        cursor: pointer;
        background-color: #dbbbe9;
        padding: 5px 10px;
        border-radius: 5px;
        font-weight: bold;
        display: flex;
        align-items: center;
        justify-content: center;
      }


      input {
        flex: 1;
        padding: 10px;
        border-radius: 5px;
        background-color: #f9f9f9;
        color: #000;
      }

      button {
        padding: 10px;
        border-radius: 5px;
        border: 1px solid #ccc;
        background-color: #000;
        color: #fff;
        cursor: pointer;
      }
    }
  }

  .correct {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background-color: #d297fd;
    cursor: pointer;
  }

}
</style>
